﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebUpdaterAdminCore.Data;
using WebUpdaterAdminCore.Models;

namespace WebUpdaterAdminCore.Controllers
{
    public class ClientListController : Controller
    {
        private ApplicationDbContext _dbContext;
        private UpdaterSocketServerService _socketServerService;


        public ClientListController(ApplicationDbContext context,
            UpdaterSocketServerService serverService)
        {
            _dbContext = context;
            _socketServerService = serverService;
        }
        public IActionResult Index()
        {
            List<SocketSessionModel> socketSessionModels = new List<SocketSessionModel>();
            if (_socketServerService != null)
            {
                socketSessionModels = _dbContext.SocketSessions.ToList();
                socketSessionModels.ForEach(model =>
                {
                    model.Connected = false;
                });
                var sessions = _socketServerService.GetAllSessions();

                bool hasChanaged = false;
                foreach (var ss in sessions)
                {
                    var clientId = _socketServerService[ss.SessionID];
                    if ("#NEW#".Equals(clientId)) continue;

                    var model = new SocketSessionModel
                    {
                        SessionId = ss.SessionID,
                        ClientId = clientId,
                        ClientName = clientId,
                        LastActiveTime = ss.LastActiveTime,
                        StartTime = ss.StartTime,
                        Connected = ss.Connected,
                        RemoteIP = ss.RemoteEndPoint.Address.ToString()
                    };
                    if (socketSessionModels.Exists(w=>w.ClientId == clientId))
                    {
                        //int idx = socketSessionModels.FindIndex(w => w.ClientId == clientId);
                        //var socketSession = socketSessionModels[idx];
                        var socketSession = socketSessionModels.Single(w => w.ClientId == clientId);
                        socketSession.SessionId = model.SessionId;
                        socketSession.ClientName = model.ClientName;
                        socketSession.Remarks = model.Remarks;
                        socketSession.LastActiveTime = model.LastActiveTime;
                        socketSession.StartTime = model.StartTime;
                        socketSession.Connected = model.Connected;
                        socketSession.RemoteIP = model.RemoteIP;

                        _dbContext.Update(socketSession);
                    }
                    else
                    {
                        hasChanaged = true;
                        socketSessionModels.Add(model);
                        _dbContext.SocketSessions.Add(model);
                    }
                }
                if (hasChanaged)
                {
                    _dbContext.SaveChanges();
                }
            }
            return View(socketSessionModels);
        }
    }
}